﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SurvivalStrategy.Modules.Memory;
using Microsoft.Xna.Framework;
using SurvivalStrategy.Entities.EnitityComponents;

namespace SurvivalStrategy.Modules.BehaviorModules
{
    class StandardMovingBehaviorModule : BehaviorModule
    {
        public StandardMovingBehaviorModule()
            : base()
        {
            AddFunction(new BehaviroModuleFunc(WanderBehavior), new BehaviroModulePriority(WanderBehaviorPriority), BehaviorTags.Standard); 
            // projektant modulu sam musi uzupelnic ta czesc
            // dopisuje tu swoje metody do listy z odpowiednimi tagami
        }

        private bool WanderBehavior()
        {
            if (GetPosition() == Vector3.Zero)
            {
                SetPosition(new Vector3(32, 0, 32));
            }

            if(GetForwardVector() == Vector3.Zero)
                SetForward(new Vector3(1, 0, 0));
            try
            {
                Vector3 forward = new Vector3(GetForwardVector().X+16, GetForwardVector().Y, GetForwardVector().Z+16);
                if (GetWorldElementRelativePosition(forward * 2).Find(o => o.Collidable) == null)   //jezeli niczego blokujacego nie ma przed nim
                {
                    SendRequest(GetID(), GetID(), ActionTags.Moving, new ActionArgs.StandardMovingArgs(forward));       //przesuwa sie do przodu
                }
                else
                {
                    Vector3 f = Vector3.Transform(forward, Matrix.CreateRotationY(-MathHelper.PiOver2));      //jezeli cos blokuje obraca sie o 90 stopni
                    SetForward(new Vector3(f.X, f.Y, f.Z));
                   
                    GetComponent<SurvivalComponent>().UseEnergy(0.01f);
                }
            }
            catch { throw new Exception("No declared world chunk at this position at: " + GetPosition().X + " " + GetPosition().Y + " " + GetPosition().Z + " "); }
         
            return false;
        }

        private int WanderBehaviorPriority()
        {
            return 0;
        }
    }
}
